﻿using System;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace WmsService.Cache
{
    public class DataCacheManager
    {
        //读取默认的CacheManager,CacheManager内部创建一个Cache对象，Cache内部维护一个Hashtable.
        public const string SessionKey = "SessionID:{0}";
        public const string UserIDKey = "I:{0}";
        public const string UserNameKey = "N:{0}";
        private static readonly ICacheManager cacheManager = CacheFactory.GetCacheManager();

        public static void SetSessionKey(string sessionID)
        {
            var key = string.Format(SessionKey, sessionID);
            cacheManager.Add(key, key);
        }

        public static bool ContainSessionKey(string sessionID)
        {
            var key = string.Format(SessionKey, sessionID);
            if (cacheManager.Contains(key))
            {
                cacheManager.GetData(key);
                return true;
            }
            return false;
        }

        public static void SetUserID(string sessionID, string userID)
        {
            var key = string.Format(UserIDKey, sessionID);
            cacheManager.Add(key, userID);
        }

        public static string GetUserID(string sessionID)
        {
            var key = string.Format(UserIDKey, sessionID);
            return (String) cacheManager.GetData(key);
        }

        public static void SetUserName(string sessionID, string userName)
        {
            var key = string.Format(UserNameKey, sessionID);
            cacheManager.Add(key, userName);
        }

        public static string GetUserName(string sessionID)
        {
            var key = string.Format(UserNameKey, sessionID);
            return (String) cacheManager.GetData(key);
        }
    }
}